package com.easybuy.web.controller;

import com.easybuy.bean.EasybuyUser;
import com.easybuy.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/index")
public class IndexFrontController {
    @Autowired
    private IUserService iNewsService;

    ///WEB-INF/index.jsp
    @RequestMapping("/index")
    public String index(){ return "index";  }


    @RequestMapping("/login")
    public String login(){
        return "login";
    }
    @RequestMapping("/loginSuf")
    public String loginSuf(EasybuyUser easybuyUser, HttpServletRequest request){
        //获取用户名密码
        String username=easybuyUser.getEuUserId();
        String password=easybuyUser.getEuPassword();
        SimpleDateFormat sdf = new SimpleDateFormat();
        Date date =new Date();
        System.out.println(easybuyUser);
        EasybuyUser e1=iNewsService.selectByPK(easybuyUser);
        if(e1!=null&&e1.getEuUserId().equals(easybuyUser.getEuUserId())&&e1.getEuPassword().equals(easybuyUser.getEuPassword())) {
            //设置session
            System.out.println("账号密码正确，可登录");
            System.out.println("e1"+e1);
//
            HttpSession session=request.getSession();
            session.setAttribute("username",username);
            session.setAttribute("nowTime",sdf.format(date));
            System.out.println("开始跳转1");
        }else{
            return "loginError";
        }
        //默认进入主页
        //return "login-result";
        System.out.println("开始跳转2");

        if(e1.getEuStatus().equals("1")){
            System.out.println("跳转到前台");
            return "login-result";
        }else if(e1.getEuStatus().equals("2")) {
            System.out.println("跳转到后台");
            return "mid";
        }
            return "login-result";

    }

    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @RequestMapping("/regSuf")
    public String regSuf(EasybuyUser easybuyUser,String EuPassword){
        System.out.println("进入检测……");
        easybuyUser.setEuStatus("1");
        if(easybuyUser.getEuSex().equals("男")){
            easybuyUser.setEuSex("T");
        }else{
            easybuyUser.setEuSex("F");
        }
        //判断用户名是否重复
        try {
            EasybuyUser e1=iNewsService.selectByPK(easybuyUser);
            e1.setEuStatus("1");
            System.out.println("e1:"+e1);
            if(e1.getEuUserId().equals(easybuyUser.getEuUserId())){
                return "regNameError";
            }else{
                System.out.println("用户名可以使用");
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        if(EuPassword.equals(easybuyUser.getEuPassword())){
            System.out.println("密码相等 可以注册");
        }else{
            return "regPasswordError";
        }
        System.out.println("注册内容"+easybuyUser);
        //注册
        iNewsService.save(easybuyUser);
        System.out.println("注册成功");
        //注册成功
        return "reg-result";
    }

    @RequestMapping("/cancellation")
    public String cancellation(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.removeAttribute("username");
        session.removeAttribute("nowTime");
        return "login";
    }

}
